<?php


namespace App\Models\traits;

use Illuminate\Database\ClassMorphViolationException;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Arr;

/**
 * Created By FreeAbrams
 * 覆盖多对多多态 morph 映射
 * Date: 2022/4/16
 */
trait Morph
{
    public function getMorphClass()
    {
    	if (isset($this->morphMap) && $this->morphMap) {
    	    return $this->morphMap;
	    }
    	
        if (Relation::requiresMorphMap()) {
            throw new ClassMorphViolationException($this);
        }
    	return static::class;
    }
    
    public static function getActualClassNameForMorph($class)
    {
        return Arr::get(self::$morphMap, $class, $class);
    }
}